package com.bestcem.xm.qdes.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.bestcem.xm.component.mybatis.service.impl.XmServiceImpl;
import com.bestcem.xm.qdes.controller.vo.DeletedCountVo;
import com.bestcem.xm.qdes.entity.QdesTag;
import com.bestcem.xm.qdes.entity.QdesTagGroup;
import com.bestcem.xm.qdes.entity.QdesTagRef;
import com.bestcem.xm.qdes.mapper.QdesTagGroupMapper;
import com.bestcem.xm.qdes.mapper.QdesTagMapper;
import com.bestcem.xm.qdes.mapper.QdesTagRefMapper;
import com.bestcem.xm.qdes.service.QdesTagService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Service
public class QdesTagServiceImpl extends XmServiceImpl<QdesTagMapper, QdesTag> implements QdesTagService {

    private @NonNull QdesTagGroupMapper qdesTagGroupMapper;

    private @NonNull QdesTagRefMapper qdesTagrefMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DeletedCountVo deleteTag(String id) {
        QdesTag qdesTag = baseMapper.selectById(id);
        //1.删除标签
        DeletedCountVo deletedCountVo = new DeletedCountVo();
        int count = baseMapper.deleteById(id);
        if (count != 0) {
            deletedCountVo.setDeletedCount(1);
            //2.修改标签组tag_ids集合-删除标签id
            QdesTagGroup qdesTaggroup = qdesTagGroupMapper.selectById(qdesTag.getGroupId());
            String tagIds = qdesTaggroup.getTagIds();

            List<String> list = JSONArray.parseArray(tagIds, String.class);
            tagIds = JSONObject.toJSONString(CollectionUtils.subtract(list, Arrays.asList(id)));
            //创建修改条件对象
            LambdaUpdateWrapper<QdesTagGroup> updateWrapper = new LambdaUpdateWrapper<>();
            //构造查询条件
            QdesTagGroup entity = new QdesTagGroup();
            entity.setTagIds(tagIds);
            updateWrapper
//                    .set(QdesTagGroup::getTagIds, tagIds)
                    .eq(QdesTagGroup::getId, qdesTag.getGroupId());
            qdesTagGroupMapper.update(entity, updateWrapper);
            //3.删除标签引用Tagref
            Map deleteMap = new HashMap<>();
            deleteMap.put("tag_id",id);
            qdesTagrefMapper.deleteByMap(deleteMap);
        } else {
            deletedCountVo.setDeletedCount(0);
        }
        return deletedCountVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QdesTag saveQdesTag(QdesTag qdesTag) {
        //1.保存标签
        baseMapper.insert(qdesTag);
        //2.修改标签组tag_ids集合-添加标签id
        QdesTagGroup qdesTaggroup = qdesTagGroupMapper.selectById(qdesTag.getGroupId());
        String tagIds = qdesTaggroup.getTagIds();
        List<String> list = JSONArray.parseArray(tagIds, String.class);
        list.add(qdesTag.getId());
        tagIds = JSONObject.toJSONString(list);
        //创建修改条件对象
        LambdaUpdateWrapper<QdesTagGroup> updateWrapper = new LambdaUpdateWrapper<>();
        //构造查询条件
        updateWrapper
                //.set(QdesTaggroup::getTagIds, tagIds)
                .eq(QdesTagGroup::getId, qdesTag.getGroupId());
        QdesTagGroup g = new QdesTagGroup();
        g.setTagIds(tagIds);
        qdesTagGroupMapper.update(g, updateWrapper);
        return qdesTag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTagName(String id,String title) {
        QdesTag qdesTag = new QdesTag();
        qdesTag.setTitle(title);
        //创建修改条件对象
        LambdaUpdateWrapper<QdesTag> updateWrapper = new LambdaUpdateWrapper<>();
        //构造查询条件
        updateWrapper
                .eq(QdesTag::getId, id);
        int i = baseMapper.update(qdesTag,updateWrapper);
        if(i != 0){
            //修改标签引用Tagref的名称
            QdesTagRef qdesTagref = new QdesTagRef();
            qdesTagref.setTagTitle(title);
            //创建修改条件对象
            LambdaUpdateWrapper<QdesTagRef> updateQdesTagref = new LambdaUpdateWrapper<>();
            //构造查询条件
            updateQdesTagref
                    .eq(QdesTagRef::getTagId, id);
            qdesTagrefMapper.update(qdesTagref,updateQdesTagref);
            return true;
        }else{
            return false;
        }
    }
}
